package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要新增的图书名字: ");
        String name = scanner.nextLine();
        System.out.println("输入你要新增的图书作者: ");
        String author = scanner.nextLine();
        System.out.println("输入你要新增的图书价格: ");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("输入你要新增的图书类型: ");
        String type = scanner.nextLine();
        //新建一个对象作为插入单位
        Book book = new Book(name, author, price, type);
        if (bookList.books.length > bookList.getUsedSize()) {
            int i = 0;
            for (; i < bookList.getUsedSize(); i++) {
                if (bookList.books[i].getName().equals(name)) {
                    System.out.println("新增失败");
                    return;
                }
            }
            bookList.books[i] = book;
            bookList.setUsedSize(bookList.getUsedSize() + 1);
            System.out.println("新增成功!");
        }

    }
}
